<?php

namespace app\index\controller;

use app\common\logic\BaseLogic;
use app\common\logic\ContentLogic;
use gdbase\Tree;
use League\Glide\Urls\UrlBuilderFactory;
use think\Controller;
use think\facade\App;
use think\Response;

class Index extends Controller
{
    public function index()
    {
//        $data = db('Category')->where('model_id',1)->select();
//        $tree = new \gdbase\Tree();
//        $tree->init($data,['child'=>'_child','parentid'=>'parent_id']);
//        $html="<select name='cat'>";
//        $html .= $tree->get_tree(0,"<option value=\$id \$selected>\$spacer\$cat_name</option>",8);
//        $html.="</select>";
//        echo $html;

//        $extend_field = [
//            ['table_name' => 'ContentContent' , 'pk' => 'c_id' , 'field' => ['content']],
//            ['table_name' => 'ContentPhotos' , 'pk' => 'c_id' , 'field' => ['photo_url']],
//        ];
//
////        echo json_encode($extend_field,256);
//
//        $sow_field = [
//            'add' => ['id','title','category_id','cover_url','description','content'],
//            'edit' => ['id','title','category_id','cover_url','description','content'],
//            'list' => ['id','title','category_id','aid'],
//        ];
//
//        echo json_encode($sow_field,256);

//        $cLogic = new ContentLogic();
//
//        dump($cLogic->getModelsExtendTableInfo(2,'edit'));

//        return 'gdbase';

        return $this->fetch();
    }

    public function hello($name = 'ThinkPHP5')
    {
        return 'hello,' . $name;
    }

    public function test()
    {
        return $this->fetch();
    }

    public function handleImageRequest()
    {
        $middleware = \Slince\Glide\GlideMiddleware::factory([
            'baseUrl' => '/images',
            'source' => App::getRootPath() . 'public/' . env('LOCAL_DIR'),
            'signKey' => '1',
            'glide' => [  // 传递给 glide 的原生参数
//                'driver' => 'imagick', // 使用imagemagic 引擎
            ],
            'onException' => function (\Exception $exception, $request, $server) {
                if ($exception instanceof \League\Glide\Signatures\SignatureException) {
                    $response = new Response('签名错误', 403);
                } else {
                    $response = new Response(sprintf('你访问的资源 "%s" 不存在', $request->path()), 404);
                }

                return $response;
            }
        ]);

        return $middleware(app('request'), function () {
            return app('response');
        });
    }

    public function test2()
    {
        /*
//        echo app('glide.url_builder')->getUrl('/20190329/7558428c11d3f6ecc3ba5cb5c3d2d07a.jpg', ['w' => 100, 'h' => 100]);
        $urlBuilder = UrlBuilderFactory::create('/images/', '1');
        $url = $urlBuilder->getUrl('20190329/abe3edd16d3c7dfc55bb64b2ca9f03e8.jpg', ['w' => 100, 'h' => 100]);
        echo $url;*/

        dump(str_replace(env('LOCAL_BASE_URL'),'','/upload/images/20190329/cee44a88575b71fb92acbf8016817b03.jpg'));
    }
}
